﻿
Imports std

Namespace config
    Public Module config
        Public WithEvents config As configMonitor = Nothing

        Public storoomhosts As vector(Of String) = Nothing
        Public storoomports As vector(Of Int32) = Nothing
        Public storoomretrytimes As Int32 = configNaming.defaultValue.raserve.storoomretrytimes
        Public ignorecrawlingtag As Boolean = configNaming.defaultValue.raserve.ignorecrawlingtag
        Public port As String = configNaming.defaultValue.raserve.port
        Public adminmail As String = configNaming.defaultValue.raserve.adminmail
        Public adminlink As String = configNaming.defaultValue.raserve.adminlink

        Public Function configRefreshing() As Boolean
            Return config.configRefreshing()
        End Function

        Public Function open(ByRef configfile As String) As Boolean
            assert(config Is Nothing, "already open a config, should call initial if refreshing.")
            config = New configMonitor()
            If config.open(configfile) Then
                Return initial()
            Else
                Return False
            End If
        End Function

        Public Function initial() As Boolean
            assert(Not config Is Nothing, "config is nothing before initial")
            If Not config.readlist(configNaming.raserve.name, configNaming.raserve.storoomhost, storoomhosts) Then
                raiseError("cannot read list of storoomhost, failed to initialize, section " + configNaming.raserve.name _
                           + ", parameter " + configNaming.raserve.storoomhost, errorHandle.errorType.exclamation)
                Return False
            End If
            If Not config.readlist(configNaming.raserve.name, configNaming.raserve.storoomport, storoomports) Then
                raiseError("cannot read list of storoomport, failed to initialize, section " + configNaming.raserve.name _
                           + ", parameter " + configNaming.raserve.storoomport, errorHandle.errorType.exclamation)
                Return False
            End If
            If storoomhosts.size() <> storoomports.size() Then
                raiseError("count of storoomhosts <> count of storoomports, ignore some to make pairs." _
                           , errorHandle.errorType.exclamation)
                If storoomhosts.size() > storoomports.size() Then
                    storoomhosts.erase(storoomports.size(), storoomhosts.size())
                    storoomports.erase(storoomhosts.size(), storoomports.size())
                End If
            End If
            storoomretrytimes = config.parameter(configNaming.raserve.name, configNaming.raserve.storoomretrytimes _
                                                  , configNaming.defaultValue.raserve.storoomretrytimes)
            ignorecrawlingtag = config.parameter(configNaming.raserve.name, configNaming.raserve.ignorecrawlingtag _
                                                  , configNaming.defaultValue.raserve.ignorecrawlingtag)
            port = config.parameter(configNaming.raserve.name, configNaming.raserve.port _
                                     , configNaming.defaultValue.raserve.port)
            adminlink = config.parameter(configNaming.raserve.name, configNaming.raserve.adminlink _
                                          , configNaming.defaultValue.raserve.adminlink)
            adminmail = config.parameter(configNaming.raserve.name, configNaming.raserve.adminmail _
                                          , configNaming.defaultValue.raserve.adminmail)

            Return True
        End Function

        Private Sub config_ConfigChanged() Handles config.ConfigChanged
            If Not initial() Then
                raiseError("failed to load config when caught changing, shutdown", errorHandle.errorType.exclamation)
                shutdown()
            Else
                restart()
            End If
        End Sub

        Private Sub config_ConfigfileDeleted() Handles config.ConfigfileDeleted
            If configMonitor.defaultConfigDeletedOperation(60 * 60 * 1000, 60 * 60) Then
                shutdown()
            End If
        End Sub
    End Module
End Namespace
